using System.Collections.Generic;
using UnityEngine;

namespace HEFramework
{
    /// <summary>
    ///
    /// UI窗口行为
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-02-14 10:36:07
    /// ----------------------------------------
    /// </summary>
    public class UIWindowBehaviour : MonoBehaviour, IUIWindowBehaviour
    {
        [Header("组件列表")] [SerializeField] private List<Transform> transforms;
        private Dictionary<string, IUIBehaviour> uiBehaviours;

        /// <summary>
        /// UI标志
        /// </summary>
        /// <returns></returns>
        private string flag;

        /// <summary>
        /// UI状态（false关闭）（true打开）
        /// </summary>
        /// <returns></returns>
        private int state;

        /// <summary>
        /// 面板Ui
        /// </summary>
        private IUICanvasBehaviour mCanvas;


        public List<Transform> Transforms => transforms;


        public string Flag
        {
            get => flag;
        }

        public int State
        {
            get => state;
            set => state = value;
        }


        /// <summary>
        /// 初始化
        /// </summary>
        public void Awake()
        {
            if (transforms == null)
            {
                transforms = new List<Transform>();
            }

            if (uiBehaviours == null)
            {
                uiBehaviours = new Dictionary<string, IUIBehaviour>();
            }
        }


        public IUIWindowBehaviour SetFlag(string _flag)
        {
            flag = _flag;
            return this;
        }


        public void Show()
        {
            state = 1;
            GetCanvas().Open();
        }

        public void Hide()
        {
            state = 0;
            GetCanvas().Close();
        }

        public void Destroy()
        {
            State = -1;
            GameObject.Destroy(this.gameObject);
        }

        public void Pop()
        {
            UILoader.Instance.PopUI(this);
        }

        public void Dispose()
        {
            UILoader.Instance.DestroyUI(this);
        }


        public IUICanvasBehaviour GetCanvas()
        {
            mCanvas = gameObject.GetComponent<IUICanvasBehaviour>();
            if (mCanvas == null)
            {
                mCanvas = gameObject.AddComponent<UICanvasBehaviourBase>();
            }

            return mCanvas;
        }

        public IUICanvasBehaviour GetCanvas(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUICanvasBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUITextBehaviour GetText(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUITextBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUITMPTextBehaviour GetTMPText(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUITMPTextBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUIImageBehaviour GetImage(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUIImageBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUIButtonBehaviour GetButton(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUIButtonBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUISliderBehaviour GetSlider(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUISliderBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUIScrollViewBehaviour GetScrollView(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUIScrollViewBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUIInputFieldBehaviour GetInputField(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUIInputFieldBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public IUITMPInputFieldBehaviour GetTMPInputField(string _name)
        {
            IUIBehaviour uiBehaviour = GetBehaviour(_name);
            if (uiBehaviour != null)
            {
                return (IUITMPInputFieldBehaviour)uiBehaviour;
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public Transform GetTransform(string _name)
        {
            for (int i = 0; i < transforms.Count; i++)
            {
                if (transforms[i].name == _name)
                {
                    return transforms[i];
                }
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public RectTransform GetRectTransform(string _name)
        {
            for (int i = 0; i < transforms.Count; i++)
            {
                if (transforms[i].name == _name)
                {
                    return (RectTransform)transforms[i];
                }
            }

            Debug.LogError(gameObject.name + "-->" + _name + " null");
            return null;
        }

        public Transform GetTransform()
        {
            return gameObject.transform;
        }

        /// <summary>
        /// 返回对象
        /// </summary>
        /// <returns></returns>
        public GameObject GetGameObject()
        {
            return gameObject;
        }

        private IUIBehaviour GetBehaviour(string _name)
        {
            IUIBehaviour uiBehaviour;
            if (uiBehaviours.TryGetValue(_name, out uiBehaviour))
            {
                return uiBehaviour;
            }

            for (int i = 0; i < transforms.Count; i++)
            {
                if (transforms[i].name == _name)
                {
                    uiBehaviour = transforms[i].GetComponent<IUIBehaviour>();
                    uiBehaviour.Awake();
                    uiBehaviours.Add(_name, uiBehaviour);
                    return uiBehaviour;
                }
            }

            return null;
        }
    }
}